package com.iring.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iring.domain.common.DeviceInfo;
import com.iring.domain.pbx.task.PBXTask;
import com.iring.exception.ParamInvalidException;
import com.iring.vo.PBXVO;
import com.iring.vo.common.BooleanResultVO;
import com.iring.vo.pbx.PBXRes;
import com.iring.vo.pbx.SyncPbxInfo;
import com.iring.vo.pbx.UploadCdr;
import com.iring.vo.pbx.UploadDevice;
import com.iring.vo.user.LoginVO;

/**
 * pbx客户端Controller
 *
 * @author wanggan
 */
@RestController
@RequestMapping("/pbx")
public class PBXController extends BaseController {

    /**
     * pbx同步任务信息接口
     *
     * @param syncPbxInfo
     *          同步任务信息参数
     * @return  需要同步的任务
     */
    @RequestMapping(value = "/sync-pbx-info", method = RequestMethod.POST,
            produces = { "application/json;charset=UTF-8" })
    public PBXTask syncPbxInfo(@RequestBody SyncPbxInfo syncPbxInfo){
        checkAuthToken();
        return pbxService.syncTaskInfo(syncPbxInfo);
    }

    /**
     * 获取pbx上传的设备信息
     *
     * @param uploadDevice
     *            {@link UploadDevice}
     * @return {@link PBXRes}
     */
    @RequestMapping(value = "/upload-device-info", method = RequestMethod.POST,
            produces = { "application/json;charset=UTF-8" })
    public PBXRes uploadDeviceInfo(@RequestBody UploadDevice uploadDevice) {
        checkAuthToken();
        return deviceService.addDevice(uploadDevice);
    }

    /**
     * 获取pbx上传的cdr信息
     *
     * @param uploadCdr
     *            {@link UploadCdr}
     * @return {@link PBXRes}
     */
    @RequestMapping(value = "/upload-call-log", method = RequestMethod.POST,
            produces = { "application/json;charset=UTF-8" })
    public PBXRes uploadCallLog(@RequestBody UploadCdr uploadCdr) {
        checkAuthToken();
        return  callLogService.saveCallLog(uploadCdr);
    }

    /**
     * 客户端检查网络是否可达，完成一次请求即可
     *
     * @return 返回字符串
     */
    @RequestMapping(value = "/check-network", method = RequestMethod.POST)
    public String checkNetwork() {
        return "okay";
    }

    /**
     * 检查用户账号是否正确
     *
     * @param loginVO
     *            用户账号信息{@link LoginVO}
     * @return 正确返回true，错误返回false
     */
    @RequestMapping(value = "/check-user-account", method = RequestMethod.POST,
            produces = { "application/json;charset=UTF-8" })
    public BooleanResultVO checkUserAccount(@RequestBody LoginVO loginVO) {
        return new BooleanResultVO(
                userService.checkPbxUserAccount(loginVO.getUsername(), loginVO.getPassword()));
    }

    /**
     * 连接vpn自动登录,返回vpnip地址和加密后的用户名
     *
     * @param mac
     *          mac地址
     * @return  vpnip和加密后的用户名
     */
    @RequestMapping(value = "/connect-pbx-info", method = RequestMethod.POST)
    public PBXVO connectPbxInfo(@RequestParam("constr") String mac) {
        DeviceInfo deviceInfo = deviceService.findByMac(mac);
        if(null == deviceInfo){
            throw new ParamInvalidException("invalid constr");
        }
        String vpnip =deviceInfo.getVpnIp();
        String encodedUsername = passwordEncoder.encodePassword(deviceInfo.getUsername());
        return new PBXVO(vpnip,encodedUsername);
    }
}
